package com.vito.oa.act.service.impl;

import static com.vito.oa.act.constant.OaActContants.CACHE_ACT_VALUE;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.act.tool.ActivitiTool;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.dao.OaActGeneralOperateMapper;
import com.vito.oa.act.dao.OaActProcessMapper;
import com.vito.oa.act.event.service.ActivitiEvent;
import com.vito.oa.act.event.service.OaActEventPublisherService;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.extend.service.OaActProgressTableService;
import com.vito.oa.act.service.OaActGeneralOperateService;

@Service
public class OaActGeneralOperateServiceImpl implements OaActGeneralOperateService {
	@Autowired 
	OaActGeneralOperateMapper generalOperateMapper;
	@Autowired
	private OaActEventPublisherService publishService;
	@Autowired
	private OaActProgressTableService actProgressTableService;
	@Autowired
	private OaActProcessMapper processMapper;
 			
	@Override
	@Cacheable(value=CACHE_ACT_VALUE, key="#root.method.name+'_'+#p0.createDept")
	public List<MyHashMap<String, String>> queryProcessListByDept(OaActExBussModel model) {		
		return generalOperateMapper.queryProcessListByDept(model);
	}
	
	@Override
	@Cacheable(value=CACHE_ACT_VALUE,key=OaActExApproveInfoModel.CACHE_KEY)
	public DataGrid<MyHashMap<String, String>> queryDeletedOrFiledData(OaActExApproveInfoModel actExApproveInfoModel) {	
		DataGrid<MyHashMap<String, String>> data = new DataGrid<MyHashMap<String,String>>();
			data.setRows(generalOperateMapper.queryDeletedOrFiledByPage(actExApproveInfoModel));
			data.setTotal(actExApproveInfoModel.getTotal());
		return data;
	}
	
	@Override
	@CacheEvict(value=CACHE_ACT_VALUE, allEntries=true)
	public Result withdrawPersonProcess(String businessId) throws Exception {
		Result rs = new Result();
		MyHashMap<String, String> processInfo = generalOperateMapper.queryProecssInfoByBusId(businessId);
		//若审批状态完结，不可撤回
		if(processInfo != null){
			String key = processInfo.get("process_key");
			//根据流程key检查是否需要发消息
			String syskey = processMapper.queryOriginalProcessKey(key);
			if(Validata.isNullOrEmpty(syskey)){
				syskey = key;
			}
			if(ActivitiEvent.END_EVENT_KEY.contains(syskey)){
				publishService.sendActEvent(businessId,syskey, key, OaActContants.PROCESS_STATUS_WITHDRAW);
			}
			//1.将申请信息上传至ftp，并将主表，从表数据跟新到历史表中；
			actProgressTableService.updateProgressToEnd(processInfo.get("process_key"),businessId,OaActContants.PROCESS_STATUS_WITHDRAW);
			//2.工作流引擎中删除该实例，
			ActivitiTool.getRuntimeService().deleteProcessInstance(processInfo.get("instance_id"), "撤回");
			rs.setCode(Result.CODE_OK);
			rs.setMsg("撤回成功");
		}else{
			rs.setCode(Result.CODE_PARA_ERROR);
			rs.setMsg("您的申请已完结，不可撤回");
		}		
		return rs;
	}
	
	@Override
	@CacheEvict(value=CACHE_ACT_VALUE, allEntries=true)
	public boolean deleteUserProcess(String businessId) throws Exception {
		boolean isSuccess = false;
		MyHashMap<String, String> processInfo = generalOperateMapper.queryProecssInfoByBusId(businessId);
		//流程中审批
		if(processInfo != null){
			String key = processInfo.get("process_key");
			//根据流程key检查是否需要发消息
			String syskey = processMapper.queryOriginalProcessKey(key);
			if(Validata.isNullOrEmpty(syskey)){
				syskey = key;
			}
			if(ActivitiEvent.END_EVENT_KEY.contains(syskey)){
				publishService.sendActEvent(businessId,syskey, key, OaActContants.PROCESS_STATUS_WITHDRAW);
			}
			if(generalOperateMapper.updateProcressDataStatus(businessId,OaActContants.PROCESS_DATA_STATUS_DELETE) > 0){
				actProgressTableService.updateProgressToEnd(processInfo.get("process_key"),businessId,OaActContants.PROCESS_STATUS_IN);
				try{
					ActivitiTool.getRuntimeService().deleteProcessInstance(processInfo.get("instance_id"), "用户删除");
				}catch(Exception e){
					e.printStackTrace();
				}
				
				isSuccess = true;
				}											
		}else{//完结审批
			isSuccess = generalOperateMapper.updateProcressDataStatus(businessId,OaActContants.PROCESS_DATA_STATUS_DELETE) > 0;
		}
		return isSuccess;

	}

	@Override
	@CacheEvict(value=CACHE_ACT_VALUE, allEntries=true)
	public boolean updateProcessToFiled(String businessId) {
		return generalOperateMapper.updateProcressDataStatus(businessId,OaActContants.PROCESS_DATA_STATUS_FILED) > 0;		
	}
	
	@Override
	public boolean canBeWithdraw(String businessId) {
		boolean withdraw = false;
		int processingCount = generalOperateMapper.queryCountOfProcessing(businessId);
		if(processingCount > 0){
			withdraw = true;
		}		
		return withdraw;
	}
}
